package com.gqj.meter.service.impl;

import com.gqj.common.constant.BusinessConstants;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.StringUtils;
import com.gqj.meter.domain.*;
import com.gqj.meter.mapper.*;
import com.gqj.meter.service.IMdChkTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 盘点任务Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdChkTaskServiceImpl implements IMdChkTaskService {
    @Autowired
    private MdChkTaskMapper mdChkTaskMapper;
    @Autowired
    private MdMeterMapper mdMeterMapper;
    @Autowired
    private MdLcEquipMapper mdLcEquipMapper;
    @Autowired
    private MdItMapper mdItMapper;
    @Autowired
    private MdComModuleMapper mdComModuleMapper;
    @Autowired
    private MdConcentratorMapper mdConcentratorMapper;
    @Autowired
    private MdOtherMaterialMapper mdOtherMaterialMapper;
    @Autowired
    private MdChkTaskDetailsMapper mdChkTaskDetailsMapper;

    /**
     * 查询盘点任务
     *
     * @param chkTaskId 盘点任务主键
     * @return 盘点任务
     */
    @Override
    public MdChkTask selectMdChkTaskByChkTaskId(Long chkTaskId) {
        return mdChkTaskMapper.selectMdChkTaskByChkTaskId(chkTaskId);
    }

    /**
     * 查询盘点任务列表
     *
     * @param mdChkTask 盘点任务
     * @return 盘点任务
     */
    @Override
    public List<MdChkTask> selectMdChkTaskList(MdChkTask mdChkTask) {
        return mdChkTaskMapper.selectMdChkTaskList(mdChkTask);
    }

    /**
     * 新增盘点任务
     *
     * @param mdChkTask 盘点任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMdChkTask(MdChkTask mdChkTask) {
        //生成盘点单号
        mdChkTask.setChkTaskNo("Pd-" + StorageUtil.getOrderNoTo12());
        mdChkTask.setStatusCode("01");
        mdChkTaskMapper.insertMdChkTask(mdChkTask);
        insertInfo(mdChkTask);
        return 1;
    }

    /**
     * 修改盘点任务
     *
     * @param mdChkTask 盘点任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMdChkTask(MdChkTask mdChkTask) {
        //不是录入状态不允许修改
        if (!"01".equals(mdChkTask.getStatusCode())) {
            throw new ServiceException("单据非录入状态，不允许修改！");
        }
        //详情表先删后插
        MdChkTaskDetails chkTaskDetails = new MdChkTaskDetails();
        chkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
        List<MdChkTaskDetails> chkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(chkTaskDetails);
        for (MdChkTaskDetails MdChkTaskDetails : chkTaskDetailsList) {
            //删除详情数据
            int i = mdChkTaskDetailsMapper.deleteMdChkTaskDetailsByTaskDetailsId(MdChkTaskDetails.getTaskDetailsId());
        }
        insertInfo(mdChkTask);
        return mdChkTaskMapper.updateMdChkTask(mdChkTask);
    }

    @Transactional
    public void insertInfo(MdChkTask mdChkTask) {
        String assetsCategory = mdChkTask.getAssetsCategory();
        String checkRange = mdChkTask.getCheckRange();
        if (assetsCategory.equals("01")) {
            //电能表
            MdChkTaskDetails mdChkTaskDetails = new MdChkTaskDetails();

            MdMeter mdM = new MdMeter();
            mdM.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
            if (BusinessConstants.CUR_STATUS_CODE_NEW.equals(mdChkTask.getCheckRange())) {
                mdM.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_NEW);
            } else if (BusinessConstants.CUR_STATUS_CODE_RETURN.equals(mdChkTask.getCheckRange())) {
                mdM.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_RETURN);
            } else {
                mdM.setCurStatusCode("");
            }
            List<MdMeter> meterList = mdMeterMapper.selectMdMeterList(mdM);

            if (meterList.size() <= 0) {
                throw new ServiceException("库内盘点范围无电能表信息，无法建立盘点单！");
            }
            for (MdMeter mdMeter : meterList) {
                mdChkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetails.setAssetsId(mdMeter.getMeterId());
                mdChkTaskDetails.setRfidNo(mdMeter.getRfidNo());
                mdChkTaskDetails.setMadeNo(mdMeter.getMadeNo());
                mdChkTaskDetails.setSortCode(mdMeter.getSortCode());
                mdChkTaskDetails.setTypeCode(mdMeter.getTypeCode());
                mdChkTaskDetails.setRemark("电能表");
                mdChkTaskDetails.setChkResult("00");
                mdChkTaskDetails.setEquipCode(mdMeter.getEquipCode());
                mdChkTaskDetails.setEquipName(mdMeter.getEquipName());
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetails);
            }
        } else if (assetsCategory.equals("02")) {//互感器
            MdIt it = new MdIt();
            it.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
            if (BusinessConstants.CUR_STATUS_CODE_NEW.equals(mdChkTask.getCheckRange())) {
                it.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_NEW);
            } else if (BusinessConstants.CUR_STATUS_CODE_RETURN.equals(mdChkTask.getCheckRange())) {
                it.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_RETURN);
            } else {
                it.setCurStatusCode("");
            }
            MdChkTaskDetails mdChkTaskDetails = new MdChkTaskDetails();

            List<MdIt> itList = mdItMapper.selectMdItList(it);

            if (itList.size() <= 0) {
                throw new ServiceException("库内盘点范围无互感器信息，无法建立盘点单！");
            }

            for (MdIt mdIt : itList) {
                mdChkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetails.setAssetsId(mdIt.getItId());
                mdChkTaskDetails.setRfidNo(mdIt.getRfidNo());
                mdChkTaskDetails.setMadeNo(mdIt.getMadeNo());
                mdChkTaskDetails.setSortCode(mdIt.getSortCode());
                mdChkTaskDetails.setTypeCode(mdIt.getTypeCode());
                mdChkTaskDetails.setRemark("互感器");
                mdChkTaskDetails.setChkResult("00");
                mdChkTaskDetails.setEquipCode(mdIt.getEquipCode());
                mdChkTaskDetails.setEquipName(mdIt.getEquipName());
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetails);
            }
        } else if (assetsCategory.equals("04")) {//采集器
            MdConcentrator concentrator = new MdConcentrator();
            concentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
            if (BusinessConstants.CUR_STATUS_CODE_NEW.equals(mdChkTask.getCheckRange())) {
                concentrator.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_NEW);
            } else if (BusinessConstants.CUR_STATUS_CODE_RETURN.equals(mdChkTask.getCheckRange())) {
                concentrator.setCurStatusCode(BusinessConstants.CUR_STATUS_CODE_RETURN);
            }
            MdChkTaskDetails mdChkTaskDetails = new MdChkTaskDetails();

            List<MdConcentrator> concentratorList = mdConcentratorMapper.selectMdConcentratorList(concentrator);

            if (concentratorList.size() <= 0) {
                throw new ServiceException("库内盘点范围无采集器信息，无法建立盘点单！");
            }

            for (MdConcentrator mdConcentrator : concentratorList) {
                mdChkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetails.setAssetsId(mdConcentrator.getConcentratorId());
                mdChkTaskDetails.setRfidNo(mdConcentrator.getRfidNo());
                mdChkTaskDetails.setMadeNo(mdConcentrator.getMadeNo());
                mdChkTaskDetails.setTypeCode(mdConcentrator.getTypeCode());
                mdChkTaskDetails.setRemark("采集器");
                mdChkTaskDetails.setChkResult("00");
                mdChkTaskDetails.setEquipCode(mdConcentrator.getEquipCode());
                mdChkTaskDetails.setEquipName(mdConcentrator.getEquipName());
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetails);
            }
        } else if (assetsCategory.equals("05")) {//通讯模块
            MdComModule comModule = new MdComModule();
            MdChkTaskDetails mdChkTaskDetails = new MdChkTaskDetails();

            List<MdComModule> comModuleList = mdComModuleMapper.selectMdComModuleList(comModule);

            if (comModuleList.size() <= 0) {
                throw new ServiceException("无通讯模块信息，无法建立盘点单！");
            }

            for (MdComModule mdComModule : comModuleList) {
                mdChkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetails.setAssetsId(mdComModule.getModuleId());
                mdChkTaskDetails.setRfidNo(mdComModule.getRfidNo());
                mdChkTaskDetails.setRemark("通讯模块");
                mdChkTaskDetails.setChkResult("00");
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetails);
            }
        } else if (assetsCategory.equals("10")) {//其他
            MdOtherMaterial otherMaterial = new MdOtherMaterial();
            MdChkTaskDetails mdChkTaskDetails = new MdChkTaskDetails();
            List<MdOtherMaterial> otherMaterialList = mdOtherMaterialMapper.selectMdOtherMaterialList(otherMaterial);

            if (otherMaterialList.size() <= 0) {
                throw new ServiceException("无其他物资信息，无法建立盘点单!");
            }

            for (MdOtherMaterial mdOtherMaterial : otherMaterialList) {
                mdChkTaskDetails.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetails.setAssetsId(mdOtherMaterial.getOtherMaterialId());
                mdChkTaskDetails.setRfidNo(mdOtherMaterial.getBarCode());
                mdChkTaskDetails.setSortCode(mdOtherMaterial.getEquipCateg());
                mdChkTaskDetails.setRemark("其他物资");
                mdChkTaskDetails.setChkResult("00");
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetails);
            }
        }
    }

    /**
     * 批量删除盘点任务
     *
     * @param chkTaskIds 需要删除的盘点任务主键
     * @return 结果
     */
    @Override
    public int deleteMdChkTaskByChkTaskIds(Long[] chkTaskIds) {

        for (Long chkTaskId : chkTaskIds) {
            MdChkTask mdChkTask = mdChkTaskMapper.selectMdChkTaskByChkTaskId(chkTaskId);
            if ("02".equals(mdChkTask.getStatusCode())) {
                throw new ServiceException("删除'" + mdChkTask.getChkTaskId() + "'任务失败，任务已执行不允许删除");
            }

            MdChkTaskDetails mdChkTaskDetailsQ = new MdChkTaskDetails();
            mdChkTaskDetailsQ.setChkTaskId(chkTaskId);
            List<MdChkTaskDetails> mdChkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(mdChkTaskDetailsQ);

            for (MdChkTaskDetails mdChkTaskDetail : mdChkTaskDetailsList) {
                //删除详情表
                mdChkTaskDetailsMapper.deleteMdChkTaskDetailsByTaskDetailsId(mdChkTaskDetail.getTaskDetailsId());
            }
        }
        return mdChkTaskMapper.deleteMdChkTaskByChkTaskIds(chkTaskIds);
    }

    /**
     * 删除盘点任务信息
     *
     * @param chkTaskId 盘点任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMdChkTaskByChkTaskId(Long chkTaskId) {
        MdChkTaskDetails chkTaskDetails = new MdChkTaskDetails();
        chkTaskDetails.setChkTaskId(chkTaskId);
        List<MdChkTaskDetails> chkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(chkTaskDetails);
        for (MdChkTaskDetails MdChkTaskDetails : chkTaskDetailsList) {
            //删除详情数据
            int i = mdChkTaskDetailsMapper.deleteMdChkTaskDetailsByTaskDetailsId(MdChkTaskDetails.getTaskDetailsId());
        }

        return mdChkTaskMapper.deleteMdChkTaskByChkTaskId(chkTaskId);
    }

    /**
     * 开始盘点
     *
     * @param chkTaskIds 需要删除的盘点任务主键
     * @return 结果
     */
    @Transactional
    public int startCheck(Long[] chkTaskIds) {
        for (int i = 0; i < chkTaskIds.length; i++) {
            Long chkTaskId = chkTaskIds[i];
            List<MdChkTaskDetails> mdChkTaskList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(new MdChkTaskDetails() {{
                setChkTaskId(chkTaskId);
            }});
            //盘点表
            MdChkTask mdChkTask = mdChkTaskMapper.selectMdChkTaskByChkTaskId(chkTaskId);
            for (MdChkTaskDetails mdChkTaskDetail : mdChkTaskList) {
                //资产大类
                if ("01".equals(mdChkTask.getAssetsCategory())) {//电能表
                    MdMeter mdMeter = mdMeterMapper.selectMdMeterByRfidNo(mdChkTaskDetail.getRfidNo());
                    mdMeter.setServiceStatus("03");
                    mdMeterMapper.updateMdMeter(mdMeter);
                } else if ("02".equals(mdChkTask.getAssetsCategory())) {//互感器
                    MdIt mdIt = mdItMapper.selectMdItByRfidNo(mdChkTaskDetail.getRfidNo());
                    mdIt.setServiceStatus("03");
                    mdItMapper.updateMdIt(mdIt);
                } else if ("03".equals(mdChkTask.getAssetsCategory())) {//采集器
                    MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByRfidNo(mdChkTaskDetail.getRfidNo());
                    mdConcentrator.setServiceStatus("03");
                    mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
                } else if ("05".equals(mdChkTask.getAssetsCategory())) {//通讯模块
                    MdComModule mdComModule = mdComModuleMapper.selectMdComModuleByRfidNo(mdChkTaskDetail.getRfidNo());
                    mdComModule.setServiceStatus("03");
                    mdComModuleMapper.updateMdComModule(mdComModule);
                } else if ("10".equals(mdChkTask.getAssetsCategory())) {//其他
                    MdOtherMaterial mdOtherMaterial = mdOtherMaterialMapper.selectMdOtherMaterialByBarCode(mdChkTaskDetail.getRfidNo());
                    mdOtherMaterial.setServiceStatus("03");
                    mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterial);
                }
            }
        }
        return mdChkTaskMapper.startCheck(chkTaskIds);
    }

    /**
     * 修改盘点结果
     *
     * @param mdChkTask 盘点任务
     * @return 结果
     */
    @Transactional
    @Override
    public int checkMdChkTask(MdChkTask mdChkTask) {
        List<String> chkRfidList = mdChkTask.getChkRfidList();
        List<String> noChkRfidList = mdChkTask.getNoChkRfidList();
        String assetsCategory = mdChkTask.getAssetsCategory();

        if (chkRfidList.size() > 0) {

            for (String rfid : chkRfidList) {
                //修改详情表盘点结果
                MdChkTaskDetails mdChkTaskDetails = mdChkTaskDetailsMapper.selectMdChkTaskDetailsByRfid(new MdChkTaskDetails() {{
                    setChkTaskId(mdChkTask.getChkTaskId());
                    setRfidNo(rfid);
                }});
                mdChkTaskDetails.setChkResult("01");
                mdChkTaskDetails.setUseEquipment(mdChkTask.getUseEquipment());
                mdChkTaskDetailsMapper.updateMdChkTaskDetails(mdChkTaskDetails);

                //修改台账状态
                switch (assetsCategory) {
                    case "01":
                        //查询台账
                        MdMeter mdMeter = mdMeterMapper.selectMdMeterByRfidNo(rfid);
                        mdMeter.setServiceStatus("01");
                        mdMeterMapper.updateMdMeter(mdMeter);
                        break;
                    case "02":
                        MdIt mdIt = mdItMapper.selectMdItByRfidNo(rfid);
                        mdIt.setServiceStatus("01");
                        mdItMapper.updateMdIt(mdIt);
                        break;
                    case "03":
                        MdLcEquip mdLcEquip = mdLcEquipMapper.selectMdLcEquipByRfidNo(rfid);
                        mdLcEquip.setServiceStatus("01");
                        mdLcEquipMapper.updateMdLcEquip(mdLcEquip);
                        break;
                    case "04":
                        MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByRfidNo(rfid);
                        mdConcentrator.setServiceStatus("01");
                        mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
                        break;
                    case "05":
                        MdComModule mdComModule = mdComModuleMapper.selectMdComModuleByRfidNo(rfid);
                        mdComModule.setServiceStatus("01");
                        mdComModuleMapper.updateMdComModule(mdComModule);
                        break;
                    case "10":
                        MdOtherMaterial mdOtherMaterial = mdOtherMaterialMapper.selectMdOtherMaterialByBarCode(rfid);
                        mdOtherMaterial.setServiceStatus("01");
                        mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterial);
                        break;
                }
            }
        }
        if (noChkRfidList.size() > 0) {
            for (String rfid : noChkRfidList) {
                //修改详情表盘点结果
                MdChkTaskDetails mdChkTaskDetails = mdChkTaskDetailsMapper.selectMdChkTaskDetailsByRfid(new MdChkTaskDetails() {{
                    setChkTaskId(mdChkTask.getChkTaskId());
                    setRfidNo(rfid);
                }});
                mdChkTaskDetails.setChkResult("02");
                mdChkTaskDetailsMapper.updateMdChkTaskDetails(mdChkTaskDetails);

                //修改台账状态
                switch (assetsCategory) {
                    case "01":
                        //查询台账
                        MdMeter mdMeter = mdMeterMapper.selectMdMeterByRfidNo(rfid);
                        mdMeter.setServiceStatus("04");
                        mdMeterMapper.updateMdMeter(mdMeter);
                        break;
                    case "02":
                        MdIt mdIt = mdItMapper.selectMdItByRfidNo(rfid);
                        mdIt.setServiceStatus("04");
                        mdItMapper.updateMdIt(mdIt);
                        break;
                    case "03":
                        MdLcEquip mdLcEquip = mdLcEquipMapper.selectMdLcEquipByRfidNo(rfid);
                        mdLcEquip.setServiceStatus("04");
                        mdLcEquipMapper.updateMdLcEquip(mdLcEquip);
                        break;
                    case "04":
                        MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByRfidNo(rfid);
                        mdConcentrator.setServiceStatus("04");
                        mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
                        break;
                    case "05":
                        MdComModule mdComModule = mdComModuleMapper.selectMdComModuleByRfidNo(rfid);
                        mdComModule.setServiceStatus("04");
                        mdComModuleMapper.updateMdComModule(mdComModule);
                        break;
                    case "10":
                        MdOtherMaterial mdOtherMaterial = mdOtherMaterialMapper.selectMdOtherMaterialByBarCode(rfid);
                        mdOtherMaterial.setServiceStatus("04");
                        mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterial);
                        break;
                }

            }
        }

//        //判断是否盘点结束
//        List<MdChkTaskDetails> mdChkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(new MdChkTaskDetails() {{
//            setChkTaskId(mdChkTask.getChkTaskId());
//        }});
//
//        if (mdChkTaskDetailsList.size() == chkRfidList.size()) {
//            mdChkTask.setStatusCode("03");
//            mdChkTaskMapper.updateMdChkTask(mdChkTask);
//        }
        mdChkTaskMapper.updateMdChkTask(mdChkTask);

        return 1;
    }


    /**
     * 新增盘点任务(工作台)
     *
     * @param mdChkTask 盘点任务
     * @return 结果
     */
    @Override
    public int insertMdChkTaskWorktop(MdChkTask mdChkTask) {
        //默认任务状态完成
        mdChkTask.setStatusCode(BusinessConstants.CHK_STATUS_CODE_END);
        int i = mdChkTaskMapper.insertMdChkTask(mdChkTask);
        if (i > 0) {
            for (MdChkTaskDetails mdChkTaskDetail : mdChkTask.getMdChkTaskDetailsList()) {
                //使用设备-工作台
                mdChkTaskDetail.setUseEquipment(BusinessConstants.USE_EQUIPMENT_WORKTOP);
                //盘点结果-正常
                mdChkTaskDetail.setChkResult(BusinessConstants.CHK_RESULT_NORMAL);
                mdChkTaskDetail.setChkTaskId(mdChkTask.getChkTaskId());
                mdChkTaskDetailsMapper.insertMdChkTaskDetails(mdChkTaskDetail);
            }
        }
        return i;
    }

    /**
     * 处理盘点明细
     *
     * @param chkTaskId 盘点任务主键，taskDetailsIds明细id
     * @return 结果
     */
    @Override
    public int checkTaskNormal(Long[] taskDetailsIds, Long chkTaskId) {
        int i;
        MdChkTask mdChkTask = mdChkTaskMapper.selectMdChkTaskByChkTaskId(chkTaskId);
        if (null == mdChkTask) {
            throw new RuntimeException("盘点任务不存在");
        }
        //根据chkTaskId查询盘点任务明细
        List<MdChkTaskDetails> mdChkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(new MdChkTaskDetails() {{
            setChkTaskId(chkTaskId);
        }});
        List<Long> taskDetailsIdList = new ArrayList<>(Arrays.asList(taskDetailsIds));
        // 遍历mdChkTaskDetailsList
        for (MdChkTaskDetails details : mdChkTaskDetailsList) {

//            //如果全不选则直接全部异常
//            if (taskDetailsIdList.size() == 0) {
//                details.setChkResult("02");
//
//                //更改物资状态
//                switch ((mdChkTask.getAssetsCategory())) {
//                    case "01":
//                        MdMeter mdMeter = mdMeterMapper.selectMdMeterByMeterId(details.getAssetsId());
//                        if (mdMeter != null) {
//                            mdMeter.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
//                            mdMeterMapper.updateMdMeter(mdMeter);
//                        }
//                        break;
//                    case "02":
//                        MdIt mdIt = mdItMapper.selectMdItByItId(details.getAssetsId());
//                        if (mdIt != null) {
//                            mdIt.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
//                            mdItMapper.updateMdIt(mdIt);
//                        }
//                        break;
//                    case "04":
//                        MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByConcentratorId(details.getAssetsId());
//                        if (mdConcentrator != null) {
//                            mdConcentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
//                            mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
//                        }
//                }
//            }
            // 判断taskDetailsId是否包含在taskDetailsIds中
            if (taskDetailsIdList.contains(details.getTaskDetailsId())) {
                // 更新状态为正常
                details.setChkResult("01");
//                //修改物资状态
//                switch ((mdChkTask.getAssetsCategory())) {
//                    case "01":
//                        MdMeter mdMeter = mdMeterMapper.selectMdMeterByMeterId(details.getAssetsId());
//                        if (mdMeter != null) {
//                            mdMeter.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
//                            mdMeterMapper.updateMdMeter(mdMeter);
//                        }
//                        break;
//                    case "02":
//                        MdIt mdIt = mdItMapper.selectMdItByItId(details.getAssetsId());
//                        if (mdIt != null) {
//                            mdIt.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
//                            mdItMapper.updateMdIt(mdIt);
//                        }
//                        break;
//                    case "04":
//                        MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByConcentratorId(details.getAssetsId());
//                        if (mdConcentrator != null) {
//                            mdConcentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
//                            mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
//                        }
//                        break;
//                }
            } else {
                //如果已经正常了则不修改
                if (!details.getChkResult().equals("01")) {
                    details.setChkResult("02");

                    //修改物资状态为盘点异常
                    switch ((mdChkTask.getAssetsCategory())) {
                        case "01":
                            MdMeter mdMeter = mdMeterMapper.selectMdMeterByMeterId(details.getAssetsId());
                            if (mdMeter != null) {
                                mdMeter.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                mdMeterMapper.updateMdMeter(mdMeter);
                            }
                            break;
                        case "02":
                            MdIt mdIt = mdItMapper.selectMdItByItId(details.getAssetsId());
                            if (mdIt != null) {
                                mdIt.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                mdItMapper.updateMdIt(mdIt);
                            }
                            break;
                        case "04":
                            MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByConcentratorId(details.getAssetsId());
                            if (mdConcentrator != null) {
                                mdConcentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
                            }
                    }
                }

            }
            i = mdChkTaskDetailsMapper.updateMdChkTaskDetails(details);
        }
        return 1;
    }

    /**
     * 结束盘点
     *
     * @param chkTaskIds 需要结束盘点任务主键集合
     * @return 结果
     */
    @Override
    public int endCheck(Long[] chkTaskIds) {
        int preChkNum = 0;
        for (Long chkTaskId : chkTaskIds) {
            //查询详情表是否有未处理数据
            List<MdChkTaskDetails> mdChkTaskDetailsList = mdChkTaskDetailsMapper.selectMdChkTaskDetailsList(new MdChkTaskDetails() {{
                setChkTaskId(chkTaskId);
            }});
            //查询单据
            MdChkTask mdChkTask = mdChkTaskMapper.selectMdChkTaskByChkTaskId(chkTaskId);
            for (MdChkTaskDetails mdChkTaskDetails : mdChkTaskDetailsList) {
                if (mdChkTaskDetails.getChkResult().equals("00")) {
                    preChkNum++;

                }
                if (preChkNum >= 1) {
                    throw new RuntimeException("存在盘点未处理数据,请先处理!");
                } else {
                    mdChkTask.setStatusCode("03");
                    mdChkTaskMapper.updateMdChkTask(mdChkTask);
                    //修改盘点物资状态
                    if (mdChkTaskDetails.getChkResult().equals("01")) {
                        switch ((mdChkTask.getAssetsCategory())) {
                            case "01":
                                MdMeter mdMeter = mdMeterMapper.selectMdMeterByMeterId(mdChkTaskDetails.getAssetsId());
                                if (mdMeter != null) {
                                    mdMeter.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
                                    mdMeterMapper.updateMdMeter(mdMeter);
                                }
                                break;
                            case "02":
                                MdIt mdIt = mdItMapper.selectMdItByItId(mdChkTaskDetails.getAssetsId());
                                if (mdIt != null) {
                                    mdIt.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);
                                    mdItMapper.updateMdIt(mdIt);
                                }
                                break;
                            case "04":
                                MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByConcentratorId(mdChkTaskDetails.getAssetsId());
                                if (mdConcentrator != null) {
                                    mdConcentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_IN);

                                }
                        }
                    } else if (mdChkTaskDetails.getChkResult().equals("02")) {
                        switch ((mdChkTask.getAssetsCategory())) {
                            case "01":
                                MdMeter mdMeter = mdMeterMapper.selectMdMeterByMeterId(mdChkTaskDetails.getAssetsId());
                                if (mdMeter != null) {
                                    mdMeter.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                    mdMeterMapper.updateMdMeter(mdMeter);
                                }
                                break;
                            case "02":
                                MdIt mdIt = mdItMapper.selectMdItByItId(mdChkTaskDetails.getAssetsId());
                                if (mdIt != null) {
                                    mdIt.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                    mdItMapper.updateMdIt(mdIt);
                                }
                                break;
                            case "04":
                                MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByConcentratorId(mdChkTaskDetails.getAssetsId());
                                if (mdConcentrator != null) {
                                    mdConcentrator.setServiceStatus(BusinessConstants.SERVICE_STATUS_CHK_ERR);
                                    mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
                                }
                        }
                    }
                }
            }
        }
        return 1;
    }
}
